<?php

namespace app\company\controller;

use app\company\logic\Export;
use think\Db;
use think\Exception;
use think\Request;
use app\api\logic\Task;

class Awardtask extends Base
{

    public $integral_type = [0 => "周", 1 => '月'];//奖扣分考核周期
    public $man_type = [1 => '日', 2 => '周', 3 => '月'];//奖扣人次周期

    public function _initialize()
    {
        parent::_initialize();
        $this->assign('integral_type', $this->integral_type);
        $this->assign('man_type', $this->man_type);
    }

    //奖扣配置
    public function config()
    {
        $id = I('id',0);
        //奖扣配置
        $list = M('com_prize_buckle_config')->where('com_id',$this->com_id)->select();
        $this->assign('list',$list);
        //获取奖扣配置详情
        if(empty($id)){
            $id = $list[0]['id']??0;
        }
        //本月配置
        $list_now = M('com_prize_buckle_config')->where('id', $id)->find();
//        var_dump($list_now);die;
        $this->assign('list_now', $list_now);
        //本月配置人员
        $users_now = M('com_prize_buckle_config_relation_users a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where('a.prize_buckle_id',$id)
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->paginate(10, false, page_param());
        $users_now_count = M('com_prize_buckle_config_relation_users a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where('a.prize_buckle_id',$id)
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->count();
        $count_page = ceil($users_now_count / 10);
        $users_list = $users_now->items();
        $users_data = [
            'list' => $users_list,
            'page' => 1,
            'count_page' => $count_page,
            'count' => $users_now_count,
            'page_limit'=>10,
        ];
        $this->assign('users_data',$users_data);
        //下月配置
        $list_next = M('com_prize_buckle_config_next')->where('config_id', $id)->find();
        $this->assign('list_next', $list_next);
        //下月配置人员
        //不参与
        $users_next_not = M('com_prize_buckle_config_relation_users_next a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where(['a.prize_buckle_id'=>$list_next['id'],'a.type'=>2])
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->select();
        $users_next_not_count = count($users_next_not);
        $this->assign('users_next_not',$users_next_not);
        $this->assign('users_next_not_count',$users_next_not_count);
        //新增
        $users_next_add = M('com_prize_buckle_config_relation_users_next a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where(['a.prize_buckle_id'=>$list_next['id'],'a.type'=>1])
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->select();
        $users_next_add_count = count($users_next_add);
        $this->assign('users_next_add_count',$users_next_add_count);
        $this->assign('users_next_add',$users_next_add);
        $this->assign('id',$id);//本月配置表id
        $this->assign('next_id',$list_next['id']);//下个月配置表id
        $this->base_department_page();
        return $this->fetch();
    }
    //分页显示数据
    public function config_users_page(){
        $limit = I('limit',10);
        $page = I('page',1);
        $id = I('id',0);
        $name = I('name','');
        $where = [];
        $where['a.prize_buckle_id'] = $id;
        if(!empty($name)){
            $where['b.name'] = ['like',"%{$name}%"];
        }
        //本月配置人员
        $users_now = M('com_prize_buckle_config_relation_users a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where($where)
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->paginate($limit, false, page_param());
        $users_now_count = M('com_prize_buckle_config_relation_users a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where($where)
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->count();
        $count_page = ceil($users_now_count / $limit);
        $users_list = $users_now->items();

        foreach ($users_list as &$value){
            if(empty($value['avatar'])){
                $value['avatar'] = '/static/avatar.jpg';
            }

        }
        $users_data = [
            'list' => $users_list,
            'page' => 1,
            'count_page' => $count_page,
            'count' => $users_now_count,
            'page_limit'=>$limit,
        ];
        return return_json(0,'成功',$users_data);
    }
    //配置接口-获取删除人员列表
    public function del_user_list(){
        $id = I('id',0);
        if(empty($id)){
            return return_json(1,'id不能为空');
        }
        $list_next = M('com_prize_buckle_config_next')->where('config_id', $id)->find();
        $users_next_not = M('com_prize_buckle_config_relation_users_next a')
                    ->where(['a.prize_buckle_id'=>$list_next['id'],'a.type'=>2])
                    ->column('a.user_id');
        $where = [];
        $where['a.prize_buckle_id'] = $id;
        if(!empty($users_next_not)){
            $where['a.user_id'] = ['not in',$users_next_not];
        }
        //人员列表
        $users = M('com_prize_buckle_config_relation_users a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where($where)
            ->field('a.user_id,b.name,b.avatar,c.name department_name')
            ->select();
        return return_json(0,'成功',$users);
    }
    //配置接口-删除本月人员(新增下月删除人员)
    public function del_user_submit(){
        $ids = I('ids/a',[]);
        $next_id = I('next_id',0);
        if(empty($ids)){
            return return_json(1,'用户id不能为空');
        }
        if(empty($next_id)){
            return return_json(1,'下个月配置表id不能为空');
        }
        $data_user = [];
        foreach ($ids as $value){
            $data_user[] = [
                'com_id'=>$this->com_id,
                'prize_buckle_id'=>$next_id,
                'user_id'=>$value,
                'type'=>2
            ];
        }
        DB::startTrans();
        try{
            M('com_prize_buckle_config_relation_users_next')->insertAll($data_user);
            $users = M('users a')
                ->join('com_department b','b.id=a.department_id','left')
                ->where('a.id','in',$ids)
                ->field('a.id user_id,a.name,a.avatar,b.name department_name')
                ->select();
            DB::commit();
            return return_json(0,'成功',$users);
        }catch (\Exception $e){
            DB::rollback();
            return return_json(1,$e->getMessage());
        }
    }
    //配置接口-删除下月人员
    public function del_next_user(){
        $ids_1 = I('ids_1/a',[]); 
        $ids_2 = I('ids_2/a',[]);
        $next_id = I('next_id',0);
        if(empty($next_id)){
            return return_json(1,'下个月配置表id不能为空');
        }
        DB::startTrans();
        try{
            if(!empty($ids_1)){
                $where = [];
                $where['type'] = 1;
                $where['user_id'] = ['in',$ids_1];
                $where['prize_buckle_id'] = $next_id;
                M('com_prize_buckle_config_relation_users_next')->where($where)->delete();
            }
            if(!empty($ids_2)){
                $where = [];
                $where['type'] = 2;
                $where['user_id'] = ['in',$ids_2];
                $where['prize_buckle_id'] = $next_id;
                M('com_prize_buckle_config_relation_users_next')->where($where)->delete();
            }
            DB::commit();
            return return_json(0);
        }catch (\Exception $e){
            DB::rollback();
            return return_json(1,$e->getMessage());
        }

    }
    //配置接口-新增人员列表
    public function add_user_list(){
        $id = I('id',0);
        if(empty($id)){
            return return_json(1,'id不能为空');
        }
        $department_id = I('department_id',0);
        $keyword = I('keyword','');
        $where = [];
        if(!empty($department_id)){
            //查询所有下级部门id
            $department_id_array[] = $department_id;
            $department_ids =  department_child($department_id_array,$this->com_id);
            $where['c.id'] = ['in',$department_ids];
        }
        if(!empty($keyword)){
            $where['b.name'] = ['like',"%{$keyword}%"];
        }
        //去除本月配置人员
        $ids_1 = M('com_prize_buckle_config_relation_users')->where(['prize_buckle_id'=>$id])->column('user_id');
        //去除已添加的下个月人员
//        $config_next = M('com_prize_buckle_config_next')->where('config_id',$id)->find();
        $ids_2 = M('com_prize_buckle_config_relation_users_next')->where(['com_id'=>$this->com_id,'type'=>1])->column('user_id');
//        var_dump($ids_2);die;
        $ids = array_merge($ids_1,$ids_2);
        if(!empty($ids)){
            $where['b.id'] = ['not in',$ids];
        }
        $where['a.prize_buckle_id'] = ['neq',0];
        $where['a.com_id'] = $this->com_id;
        $users = M('com_prize_buckle_relation_users a')
            ->join('users b','b.id=a.user_id','left')
            ->join('com_department c','c.id=b.department_id','left')
            ->where($where)
            ->field('a.user_id,b.name,b.avatar,b.job_number,c.name department_name')
            ->select();
        return return_json(0,'成功',$users);

        //有奖扣权限的
        //部门分类id下面的
        //是否有搜索用户模糊匹配


    }
    //配置接口-提交添加人员
    public function add_user_submit(){
        $ids = I('ids/a',[]);
        $next_id = I('next_id',0);
        if(empty($next_id)){
            return return_json(1,'下个月配置表id不能为空');
        }
        DB::startTrans();
        try{
            if(!empty($ids)){
                $data_user = [];
                foreach ($ids as $value){
                    $data_user[] = [
                        'com_id'=>$this->com_id,
                        'prize_buckle_id'=>$next_id,
                        'user_id'=>$value,
                        'type'=>1
                    ];
                }
                M('com_prize_buckle_config_relation_users_next')->insertAll($data_user);
            }
            $users = M('users a')
                ->join('com_department b','b.id=a.department_id','left')
                ->where('a.id','in',$ids)
                ->field('a.id user_id,a.name,a.avatar,b.name department_name')
                ->select();
            DB::commit();
            return return_json(0,'成功',$users);
        }catch (\Exception $e){
            DB::rollback();
            return return_json(1,$e->getMessage());
        }
    }




    //奖扣配置编辑
    public function config_edit(Request $request)
    {
        if ($request->isPost()) {
            return $this->config_store($request);
        }
        $id = I('id', 0);
        $list = M('com_prize_buckle_config')->where('id', $id)->find();
        $this->assign('info', $list);
        return $this->fetch();
    }

    //奖扣配置编辑或添加提交
    public function config_store()
    {
        $params = input();
//        var_dump($params);die;
        $result = $this->validate(
            $params,
            ['name' => 'require|max:50',
            ],
            ['name.require' => '标题必填',
                'name.max' => '标题最多50个字符',
            ]
        );
        if (true !== $result) {
            return $this->ajaxReturn(1, $result);
        }
        if (isset($params['type'])) {
            if (in_array(1, $params['type'])) {
                $params['is_integral'] = 1;
            } else {
                $params['is_integral'] = 0;
            }
            if (in_array(2, $params['type'])) {
                $params['is_rate'] = 1;
            } else {
                $params['is_rate'] = 0;
            }
            if (in_array(3, $params['type'])) {
                $params['is_man'] = 1;
            } else {
                $params['is_man'] = 0;
            }
            unset($params['type']);
        }
        if ($params['id']) {
            db('com_prize_buckle_config')->where('id', $params['id'])->update($params);
        } else {
            $params['com_id'] = $this->com_id;
            $params['create_time'] = time();
            Db::startTrans();
            try {
                $id = db('com_prize_buckle_config')->where('id', $params['id'])->insertGetId($params);
                $params['config_id'] = $id;
                unset($params['name']);
                db('com_prize_buckle_config_next')->where('id', $params['id'])->insert($params);
                Db::commit();
                return $this->ajaxReturn(0, "成功", url(CONTROLLER_NAME . '/config'));
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return $this->ajaxReturn('0', $e->getMessage());
            }
        }

    }

    //改变状态
    public function change_status()
    {
        $id = I('id', 0);
        if(empty($id)){
            return $this->ajaxReturn(1,'配置id不能为空');
        }
        $users = M('com_prize_buckle_config_next')->where('config_id',$id)->find();
        $users_list_a = db('com_prize_buckle_config_relation_users')->where('prize_buckle_id', $id)->delete();
        $users_list = M('com_prize_buckle_config_relation_users_next')->where('prize_buckle_id',$users['id'])->find();
        if(!empty($users_list) || !empty($users_list_a)){
            return $this->ajaxReturn(1,'已设置人员不能删除');
        }
        DB::startTrans();
        try{
            db('com_prize_buckle_config')->where('id', $id)->delete();
            db('com_prize_buckle_config_next')->where('config_id', $id)->delete();
            DB::commit();
            return $this->ajaxReturn(0, '成功');
        }catch (\Exception $e){
            DB::rollback();
            return $this->ajaxReturn(1,$e->getMessage());
        }
        return $this->ajaxReturn(0, '成功');
    }

    //奖扣配置人员列表
    public function config_user()
    {
        $id = I('id', 0);
        $list = M('com_prize_buckle_config_relation_users a')
            ->join('users b', 'b.id = a.user_id', 'left')
            ->join('com_department c', 'c.id = b.department_id', 'left')
            ->where('a.prize_buckle_id', $id)
            ->field('a.id,b.avatar,b.name,c.name department_name')
            ->paginate('', false, page_param());
        $this->assign('list', $list);
        $this->assign('id', $id);
        return $this->fetch();
    }

    //新增配置人员
    public function user_edit(Request $request)
    {
        if ($request->isPost()) {
            return $this->user_store($request);
        }
        $id = I('id', 0);
        $com_id = $this->com_id;
        $ids = M('com_prize_buckle_config_relation_users')->where('com_id', $com_id)->column('user_id');
        $where['id'] = ['not in', $ids];
        $where['com_id'] = $com_id;
        $ids_1 = M('com_prize_buckle_relation_users')->where('com_id', $com_id)->column('user_id');
        $where_1['id'] = ['in', $ids_1];
        $list = M('users')->where($where)->where($where_1)->select();
        $this->assign('user_list', $list);
        $this->assign('id', $id);
        return $this->fetch();
    }

    //新增配置人员提交
    public function user_store()
    {
        $params = input();
        $result = $this->validate(
            $params,
            ['user_id' => 'require',
            ],
            ['user_id.require' => '用户id不能为空',
            ]
        );
        if (true !== $result) {
            return $this->ajaxReturn(1, $result);
        }
        $params['com_id'] = $this->com_id;
        $params['prize_buckle_id'] = $params['id'];
        unset($params['id']);
        db('com_prize_buckle_config_relation_users')->insert($params);
        return $this->ajaxReturn(0, "成功", url(CONTROLLER_NAME . '/config_user', ['id' => $params['prize_buckle_id']]));
    }

    //删除
    public function user_del()
    {
        $params = input();
        $ids = explode(',', $params['id']);
        db('com_prize_buckle_config_relation_users')->where('id', 'in', $ids)->delete();
        return $this->ajaxReturn(0, '成功');
    }

    //下个月奖扣配置编辑
    public function config_next_edit(Request $request)
    {
        if ($request->isPost()) {
            return $this->config_next_store($request);
        }
        $id = I('id', 0);
        $list = M('com_prize_buckle_config_next a')
            ->join('com_prize_buckle_config b', 'a.config_id = b.id', 'left')
            ->field('a.*,b.name')
            ->where('a.config_id', $id)->find();
        $this->assign('info', $list);
        return $this->fetch();
    }

    //下个月奖扣配置编辑或添加提交
    public function config_next_store()
    {
        $params = input();
//        var_dump($params);die;
        if (isset($params['type'])) {
            if (in_array(1, $params['type'])) {
                $params['is_integral'] = 1;
            } else {
                $params['is_integral'] = 0;
            }
            if (in_array(2, $params['type'])) {
                $params['is_rate'] = 1;
            } else {
                $params['is_rate'] = 0;
            }
            if (in_array(3, $params['type'])) {
                $params['is_man'] = 1;
            } else {
                $params['is_man'] = 0;
            }
            unset($params['type']);
        }
        unset($params['name']);
        db('com_prize_buckle_config_next')->where('id', $params['id'])->update($params);
        return $this->ajaxReturn(0, "成功", url(CONTROLLER_NAME . '/config'));
    }

    //下个月奖扣配置人员列表
    public function config_user_next()
    {
        $id = I('id', 0);
        $aid = I('aid', 0);
        if (empty($aid)) {
            $aid = M('com_prize_buckle_config_next')->where('config_id', $id)->value('id');
        }
        $list = M('com_prize_buckle_config_relation_users_next a')
            ->join('users b', 'b.id = a.user_id', 'left')
            ->join('com_department c', 'c.id = b.department_id', 'left')
            ->where('a.prize_buckle_id', $aid)
            ->field('a.id,b.avatar,b.name,c.name department_name')
            ->paginate('', false, page_param());
        $this->assign('list', $list);
        $this->assign('id', $aid);
        return $this->fetch();
    }

    //下个月新增配置人员
    public function user_next_edit(Request $request)
    {
        if ($request->isPost()) {
            return $this->user_next_store($request);
        }
        $id = I('id', 0);
        $com_id = $this->com_id;
        $ids = M('com_prize_buckle_config_relation_users_next')->where('com_id', $com_id)->column('user_id');
        $where['id'] = ['not in', $ids];
        $where['com_id'] = $com_id;
        $list = M('users')->where($where)->select();
        $this->assign('user_list', $list);
        $this->assign('id', $id);
        return $this->fetch();
    }

    //新增配置人员提交
    public function user_next_store()
    {
        $params = input();
        $result = $this->validate(
            $params,
            ['user_id' => 'require',
            ],
            ['user_id.require' => '用户id不能为空',
            ]
        );
        if (true !== $result) {
            return $this->ajaxReturn(1, $result);
        }
        $params['com_id'] = $this->com_id;
        $params['prize_buckle_id'] = $params['id'];
        unset($params['id']);
        db('com_prize_buckle_config_relation_users_next')->insert($params);
        return $this->ajaxReturn(0, "成功", url(CONTROLLER_NAME . '/config_user_next', ['aid' => $params['prize_buckle_id']]));
    }

    //删除
    public function user_next_del()
    {
        $params = input();
        $ids = explode(',', $params['id']);
        db('com_prize_buckle_config_relation_users_next')->where('id', 'in', $ids)->delete();
        return $this->ajaxReturn(0, '成功');
    }

    public function config_info()
    {
        $id = I('id', 0);
        $list = M('com_prize_buckle_config')->where('id', $id)->find();
        $this->assign('info', $list);
        $list_a = M('com_prize_buckle_config_next')->where('config_id', $list['id'])->find();
        $this->assign('info_a', $list_a);
        return $this->fetch();
    }
    //奖扣任务查看
    public function record()
    {
        $time = I('time',date('Y-m',time()));
        $name = I('name','');
        $task_name = I('task_name','');
        $department_id = I('department_id',0);
        $where = [];
        $where['a.com_id'] = $this->com_id;
        if(!empty($department_id)){
            $department_id_1[] = $department_id;
            $department_ids = department_child($department_id_1,$this->com_id);
            $where['d.id'] = ['in',$department_ids];
        }
        if(!empty($name)){
            $where['c.name'] = ['like',"%{$name}%"];
        }
        if($time == date('Y-m',time())){//当月奖扣任务
            if(!empty($task_name)){
                $where['b.name'] = ['like',"%{$task_name}%"];
            }
            if(input('export') == 1){
                $list = M('com_prize_buckle_config_relation_users a')
                    ->join('com_prize_buckle_config b', 'a.prize_buckle_id = b.id', 'left')
                    ->join('users c', 'a.user_id = c.id', 'left')
                    ->join('com_department d', 'c.department_id = d.id', 'left')
                    ->field('a.user_id,c.job_number,d.name department_name,b.*,b.name prize_name,c.name')
                    ->where($where)
                    ->select();
                foreach ($list as $key=>$value){
                    //计算本月扣分
                    $info = Task::get_prize_buckle_task($value['user_id'],$time);
                    $list[$key]['total_del_integral'] = -abs($info['total_del_integral']); //预计扣分
                    $list[$key]['real_del_integral'] = 0; //实际扣分
                    $list[$key]['time'] = strtotime($time.'-01'); //时间
                    $list[$key]['prize_integral_com'] = $info['prize_integral_com']; //奖分完成值
                    $list[$key]['buckle_integral_com'] = $info['buckle_integral_com']; //扣分完成值
                    $list[$key]['total_people'] = $info['total_people']; //人次完成值
                    $list[$key]['people_com_count'] = $info['people_com_count']; //人次完成次数
//                    $list[$key]['people_type'] = $info['people_type']; //人次类型  1日任务  2周任务  3月任务
                    if($info['people_type'] == 1){
                        $list[$key]['people_type'] = '日任务';
                    }elseif ($info['people_type'] == 2){
                        $list[$key]['people_type'] = '周任务';
                    }else{
                        $list[$key]['people_type'] = '月任务';
                    }
                    $list[$key]['rate_com'] = $info['rate_com']; //月完成比例
                    $list[$key]['prize_integral_del'] = $info['prize_integral_del']; //奖分未完成扣分
                    $list[$key]['buckle_integral_del'] = $info['buckle_integral_del']; //扣分未完成扣分
                    $list[$key]['people_del_total'] = $info['people_del_total']; //人次未完成扣分
                    $list[$key]['rate_del'] = $info['rate_del']; //比例未完成扣分
                }
                $this->record_explode($list);

            }
            $list = M('com_prize_buckle_config_relation_users a')
                ->join('com_prize_buckle_config b', 'a.prize_buckle_id = b.id', 'left')
                ->join('users c', 'a.user_id = c.id', 'left')
                ->join('com_department d', 'c.department_id = d.id', 'left')
                ->field('a.user_id,c.job_number,d.name department_name,b.*,b.name prize_name,c.name')
                ->where($where)
                ->paginate('', false, page_param());
            $res = $list->items();
            foreach ($res as $key=>$value){
                //计算本月扣分
                $info = Task::get_prize_buckle_task($value['user_id'],$time);
                $res[$key]['total_del_integral'] = -abs($info['total_del_integral']); //预计扣分
                $res[$key]['real_del_integral'] = 0; //实际扣分
                $res[$key]['time'] = strtotime($time.'-01'); //时间

            }
        }else{
            $where['a.year_month'] = strtotime($time);
            if(!empty($task_name)){
                $where['a.prize_buckle_task_name'] = ['like',"%{$task_name}%"];
            }
            if(input('export') == 1){
                $list = M('com_prize_buckle_task_log a')
                    ->join('users c', 'a.user_id = c.id', 'left')
                    ->join('com_department d', 'c.department_id = d.id', 'left')
                    ->where($where)
                    ->field('a.*,c.job_number,d.name department_name,c.name')
                    ->select();
                foreach ($list as $key=>$value){
                    $list[$key]['real_del_integral'] = -abs($value['total_del_integral']); //实际扣分
                    $list[$key]['total_del_integral'] = -abs($value['total_del_integral']); //预计扣分
                    $list[$key]['prize_name'] = $value['prize_buckle_task_name']; //任务名称
                    $list[$key]['integral_reward'] = $value['prize_integral_target']; //奖分目标
                    $list[$key]['integral_deduct'] = $value['buckle_integral_target']; //扣分目标
                    $list[$key]['integral_type'] = $value['prize_integral_type']; //奖扣类型
                    $list[$key]['man_num'] = $value['people_target']; //人次目标
                    $list[$key]['man_type'] = $value['people_type']; //人次类型
                    $list[$key]['rate'] = $value['rate_target']; //比例目标
                    $list[$key]['time'] = strtotime($time.'-01'); //时间
                    $list[$key]['prize_integral_com'] = $value['total_prize_integral']; //奖分完成值
                    $list[$key]['buckle_integral_com'] = $value['total_buckle_integral']; //扣分完成值
                    $list[$key]['total_people'] = $value['total_people']; //人次完成值
                    $list[$key]['people_com_count'] = 0; //人次完成次数
//                    $list[$key]['people_type'] = $value['people_type']; //人次类型  1日任务  2周任务  3月任务
                    if($value['people_type'] == 1){
                        $list[$key]['people_type'] = '日任务';
                    }elseif ($value['people_type'] == 2){
                        $list[$key]['people_type'] = '周任务';
                    }else{
                        $list[$key]['people_type'] = '月任务';
                    }
                    $list[$key]['rate_com'] = $value['total_rate']; //月完成比例
                    $list[$key]['prize_integral_del'] = $value['prize_integral_del']; //奖分未完成扣分
                    $list[$key]['buckle_integral_del'] = $value['buckle_integral_del']; //扣分未完成扣分
                    $list[$key]['people_del_total'] = $value['people_del']; //人次未完成扣分
                    $list[$key]['rate_del'] = $value['rate_del']; //比例未完成扣分

                }
                $this->record_explode($list);
            }
            $list = M('com_prize_buckle_task_log a')
                ->join('users c', 'a.user_id = c.id', 'left')
                ->join('com_department d', 'c.department_id = d.id', 'left')
                ->where($where)
                ->field('a.*,c.job_number,d.name department_name,c.name')
                ->paginate('', false, page_param());
            $res = $list->items();
            foreach ($res as $key=>$value){
                $res[$key]['real_del_integral'] = -abs($value['total_del_integral']); //实际扣分
                $res[$key]['prize_name'] = $value['prize_buckle_task_name']; //任务名称
                $res[$key]['integral_reward'] = $value['prize_integral_target']; //奖分目标
                $res[$key]['integral_deduct'] = $value['buckle_integral_target']; //扣分目标
                $res[$key]['integral_type'] = $value['prize_integral_type']; //奖扣类型
                $res[$key]['man_num'] = $value['people_target']; //人次目标
                $res[$key]['man_type'] = $value['people_type']; //人次类型
                $res[$key]['rate'] = $value['rate_target']; //比例目标
                $res[$key]['time'] = strtotime($time.'-01'); //时间
            }
        }
        $this->base_department_page();
        $this->assign('res',$res);
        $this->assign('list', $list);
        return $this->fetch();
    }
    //奖扣任务导出excel
    public function record_explode($data){
        require EXTEND_PATH.'Export.php';
//        var_dump($data);
        $key = [
            '姓名'
            , '工号'
            , '机构'
            , '考核时间'
            , '任务名称'
            , '月完成奖分'
            , '月完成扣分'
            , '月完成人次'
            , '人次任务完成次数'
            ,'人次任务周期'
            ,'月完成比例(扣分/奖分%)'
            ,'奖分任务未完成扣分'
            ,'扣分任务未完成扣分'
            ,'奖扣人次未完成扣分'
            ,'奖扣比例未完成扣分'
            ,'预计月总扣分'
            ,'本月实际总扣分'
        ];
        $val = [];
        foreach ($data as $k => $v) {
            $val[] = [
                $v['name'],
                $v['job_number'],
                $v['department_name'],
                date('Ym',$v['time']),
                $v['prize_name'],
                $v['prize_integral_com'],
                $v['buckle_integral_com'],
                $v['total_people'],
                $v['people_com_count'],
                $v['people_type'],
                $v['rate_com'],
                $v['prize_integral_del'],
                $v['buckle_integral_del'],
                $v['people_del_total'],
                $v['rate_del'],
                $v['total_del_integral'],
                $v['real_del_integral'],
            ];
        }
        Export::excel($key,$val,'奖扣任务列表',20,['D']);
//        new \Export($key, $val, '奖扣任务列表');
        die;
    }

    //我的奖扣任务
    public function index()
    {
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $time = I('time', date('Y-m', time()));
        //用户信息
        $users = M('users a')
            ->join('com_department b', 'a.department_id = b.id', 'left')
            ->where('a.id', $uid)
            ->field('a.name,a.avatar,a.job_number,b.name department_name')
            ->find();
        if ($time == date('Y-m', time())) {//本月
            $data = Task::get_prize_buckle_task($uid, $time);
            $data_total = [
                'prize_integral_target' => $data['prize_integral_target'],//奖分目标
                'buckle_integral_target' => $data['buckle_integral_target'],//扣分目标
                'people_target' => $data['people_target'],//人次目标
                'rate_target' => $data['rate_target'],//比例目标
                'prize_integral_com' => $data['prize_integral_com'],//总奖分
                'buckle_integral_com' => $data['buckle_integral_com'],//总扣分
                'people_com' => $data['people_com'],//总人次
                'rate_com' => $data['rate_com'],//总比例
                'prize_integral_del' => $data['prize_integral_del'],//奖分预计扣分
                'buckle_integral_del' => $data['buckle_integral_del'],//扣分预计扣分
                'people_del_total' => $data['people_del_total'],//人次预计扣分
                'rate_del' => $data['rate_del'],//比例预计扣分
                'prize_integral_del_real' => 0,//奖分实际扣分
                'buckle_integral_del_real' => 0,//扣分实际扣分
                'people_del_total_real' => 0,//人次实际扣分
                'rate_del_real' => 0,//比例实际扣分
                'total_del_integral' => $data['total_del_integral'],//总扣分

            ];
            //比例任务
            $data_rate = [
                'rate_del' => $data['rate_del'],//预计扣分
                'prize_integral_com' => $data['prize_integral_com'],//奖分
                'buckle_integral_com' => $data['buckle_integral_com'],//扣分
                'rate_com' => $data['rate_com'],//已完成比例
                'rate_target' => $data['rate_target'],//比例目标
            ];
        } else {
            $time_1 = strtotime($time);
            $data = M('com_prize_buckle_task_log')->where(['user_id' => $uid, 'year_month' => $time_1])->find();
            if (!empty($data)) {
                $data_total = [
                    'prize_integral_target' => $data['prize_integral_target'],//奖分目标
                    'buckle_integral_target' => $data['buckle_integral_target'],//扣分目标
                    'people_target' => $data['people_target'],//人次目标
                    'rate_target' => $data['rate_target'],//比例目标
                    'prize_integral_com' => $data['total_prize_integral'],//总奖分
                    'buckle_integral_com' => $data['total_buckle_integral'],//总扣分
                    'people_com' => $data['total_people'],//总人次
                    'rate_com' => $data['total_rate'],//总比例
                    'prize_integral_del' => $data['prize_integral_del'],//奖分预计扣分
                    'buckle_integral_del' => $data['buckle_integral_del'],//扣分预计扣分
                    'people_del_total' => $data['people_del'],//人次预计扣分
                    'rate_del' => $data['rate_del'],//比例预计扣分
                    'prize_integral_del_real' => $data['prize_integral_del'],//奖分实际扣分
                    'buckle_integral_del_real' => $data['buckle_integral_del'],//扣分实际扣分
                    'people_del_total_real' => $data['people_del'],//人次实际扣分
                    'rate_del_real' => $data['rate_del'],//比例实际扣分
                    'total_del_integral' => $data['total_del_integral'],//总扣分
                ];
                //比例任务
                $data_rate = [
                    'rate_del' => $data['rate_del'],//预计扣分
                    'prize_integral_com' => $data['total_prize_integral'],//奖分
                    'buckle_integral_com' => $data['total_buckle_integral'],//扣分
                    'rate_com' => $data['total_rate'],//已完成比例
                    'rate_target' => $data['rate_target'],//比例目标
                ];
            } else {
                $data_total = [
                    'prize_integral_target' => 0,//奖分目标
                    'buckle_integral_target' => 0,//扣分目标
                    'people_target' => 0,//人次目标
                    'rate_target' => 0,//比例目标
                    'prize_integral_com' => 0,//总奖分
                    'buckle_integral_com' => 0,//总扣分
                    'people_com' => 0,//总人次
                    'rate_com' => 0,//总比例
                    'prize_integral_del' => 0,//奖分预计扣分
                    'buckle_integral_del' => 0,//扣分预计扣分
                    'people_del_total' => 0,//人次预计扣分
                    'rate_del' => 0,//比例预计扣分
                    'prize_integral_del_real' => 0,//奖分实际扣分
                    'buckle_integral_del_real' => 0,//扣分实际扣分
                    'people_del_total_real' => 0,//人次实际扣分
                    'rate_del_real' => 0,//比例实际扣分
                    'total_del_integral' => 0,//总扣分
                ];
                $data_rate = [
                    'rate_del' => 0,//预计扣分
                    'prize_integral_com' => 0,//奖分
                    'buckle_integral_com' => 0,//扣分
                    'rate_com' => 0,//已完成比例
                    'rate_target' => 0,//比例目标
                ];
            }
        }
        //人次任务
        $data_people = Task::get_people_task($uid, $time);
//        var_dump($data_people);die;
        //奖扣分任务
        $data_prize_buckle = Task::get_prize_buckle_integral_task($uid, $time);
//        var_dump($data_prize_buckle);die;
        $people_total = 0;
        foreach ($data_people['list'] as $key111=>$value111){
            $people_total += $value111['people_sum'];
        }
        $data_total['people_com'] = $people_total;
        $data_all = [
            'users' => $users,//用户数据
            'data_total' => $data_total,//统计数据
            'data_rate' => $data_rate,//比例数据
            'data_people' => $data_people,//人次数据
            'data_prize_buckle' => $data_prize_buckle,//奖扣数据
        ];
        $this->assign('data_all', $data_all);
//        var_dump($data_all);die;
        return $this->fetch();
    }

    //奖分任务详情
    public function index_info()
    {
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $type = I('type', 1); //类型 1月  2周  3天  默认1
        $time = I('time',''); //默认本年时间
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b', 'a.prize_buckle_id=b.id', 'left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        if ($type == 1) {//月类型
            if(empty($time)){
                $time = date('Y',time());
            }
            $start = strtotime($time . '-01-01');
            $end = strtotime($time . '-12-31') + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $where['a.integral_b'] = ['gt', 0]; //奖分
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();

            foreach ($res_people as $key => $value) {
                $res_people[$key]['month'] = (int)date('m', $value['prize_buckle_time']);
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            for ($i = 1; $i <= 12; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['month'] == $i) {
                        $data_array[] = $v;
                        $prize += $v['integral_b'];
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '月',
                    'integral_type' => $sys['integral_type'], //奖分类型 0周任务  1月任务
                    'integral_reward' => $sys['integral_reward'], //奖分目标
                    'data_array' => $data_array
                ];
            }
        } elseif ($type == 2) {//周类型
            if(empty($time)){
                $time = date('Y-m',time());
            }
            $start = strtotime($time . '-01');
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $where['a.integral_b'] = ['gt', 0]; //奖分
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();
            $week_time_1 = $start + 3600 * 24 * 7;
            $week_time_2 = $week_time_1 + 3600 * 24 * 7;
            $week_time_3 = $week_time_2 + 3600 * 24 * 7;
            foreach ($res_people as $key => $value) {
                if ($value['prize_buckle_time'] < $week_time_1) {
                    $res_people[$key]['week'] = 1;
                } elseif ($value['prize_buckle_time'] >= $week_time_1 && $value['prize_buckle_time'] < $week_time_2) {
                    $res_people[$key]['week'] = 2;
                } elseif ($value['prize_buckle_time'] >= $week_time_2 && $value['prize_buckle_time'] < $week_time_3) {
                    $res_people[$key]['week'] = 3;
                } else {
                    $res_people[$key]['week'] = 4;
                }
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            for ($i = 1; $i <= 4; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['week'] == $i) {
                        $data_array[] = $v;
                        $prize += $v['integral_b'];
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '周',
                    'integral_type' => $sys['integral_type'], //奖分类型 0周任务  1月任务
                    'integral_reward' => $sys['integral_reward'], //奖分目标
                    'data_array' => $data_array
                ];
            }
        } elseif ($type == 3) {//天类型
            if(empty($time)){
                $time = date('Y-m',time());
            }
            $start = strtotime($time . '-01');
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $where['a.integral_b'] = ['gt', 0]; //奖分
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();
            foreach ($res_people as $key => $value) {
                $res_people[$key]['day'] = (int)date('d', $value['prize_buckle_time']);
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            $day = (int)date('d', strtotime('+1 month -1 day', strtotime($time . '-01')));
            for ($i = 1; $i <= $day; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['day'] == $i) {
                        $data_array[] = $v;
                        $prize += $v['integral_b'];
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '日',
                    'integral_type' => $sys['integral_type'], //奖分类型 0周任务  1月任务
                    'integral_reward' => $sys['integral_reward'], //奖分目标
                    'data_array' => $data_array
                ];
            }
        }
        $users = M('users a')->join('com_department b','b.id=a.department_id','left')
            ->where('a.id',$uid)
            ->field('a.name,b.name department_name,a.job_number')->find();
//        var_dump($data);die;
        //当前月份奖扣详情
        $where = [];
        $time = date('Y-m',time());
        $start = strtotime($time . '-01');
        $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.status'] = 2; //审核通过
        $where['a.integral_b'] = ['gt', 0]; //奖分
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate(10, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $sum = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->sum('a.integral_b');
        $count_page = ceil($count / 10);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $month_data = [
            'list' => $list,
            'page' => 1,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>10,
            'sum'=>$sum,
        ];
        $this->assign('month_data',$month_data);
        $this->assign('data', $data);
        $this->assign('users', $users);
        return $this->fetch();
    }
    //奖分详情接口数据
    public function prize_info_api(){
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $type = I('type',1);//类型  1月份  2周  3天
        $time = I('time','');
        $number = I('number','');
        $page = I('page',1);
        $page_limit = I('page_limit',10);
        if(empty($type)){
            return return_json(1,'类型不能为空');
        }
        if(empty($time)){
            return return_json(1,'时间不能为空');
        }
        if(empty($number)){
            return return_json(1,'数字不能为空');
        }
        $where = [];
        if($type == 1){
            $start = strtotime($time . '-'.$number);
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        }elseif($type == 2){
            if($number == 1){
                $start = strtotime($time . '-01');
                $end = $start + 3600 * 24 * 7;
            }elseif ($number == 2){
                $start = strtotime($time . '-01') + 3600 * 24 * 7;
                $end = $start + 3600 * 24 * 7;
            }elseif ($number == 3){
                $start = strtotime($time . '-01') + 3600 * 24 * 14;
                $end = $start + 3600 * 24 * 21;
            }elseif ($number == 4){
                $start = strtotime($time . '-01') + 3600 * 24 * 21;
                $end = strtotime('+1 month -1 day',strtotime($time . '-01')) + 24 * 3600 - 1;
            }

        }elseif ($type == 3){
            $start = strtotime($time . '-'.$number);
            $end = $start + 3600 * 24 -1;
        }
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.status'] = 2; //审核通过
        $where['a.integral_b'] = ['gt', 0]; //奖分
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate($page_limit, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $count_page = ceil($count / $page_limit);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $data = [
            'list' => $list,
            'page' => (int)$page,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>$page_limit,
        ];
        return return_json(0, '获取成功', $data);

    }

    //扣分任务详情
    public function buckle_info()
    {
        $type = I('type', 1); //类型 1月  2周  3天  默认1
        $time = I('time',''); //默认本年时间
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b', 'a.prize_buckle_id=b.id', 'left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        if ($type == 1) {//月类型
            if(empty($time)){
                $time = date('Y',time());
            }
            $start = strtotime($time . '-01-01');
            $end = strtotime($time . '-12-31') + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $where['a.integral_b'] = ['lt', 0]; //扣分
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();

            foreach ($res_people as $key => $value) {
                $res_people[$key]['month'] = (int)date('m', $value['prize_buckle_time']);
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            for ($i = 1; $i <= 12; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['month'] == $i) {
                        $data_array[] = $v;
                        $prize += $v['integral_b'];
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '月',
                    'integral_type' => $sys['integral_type'], //奖分类型 0周任务  1月任务
                    'integral_reward' => $sys['integral_deduct'], //扣分目标
                    'data_array' => $data_array
                ];
            }
        } elseif ($type == 2) {//周类型
            if(empty($time)){
                $time = date('Y-m',time());
            }
            $start = strtotime($time . '-01');
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $where['a.integral_b'] = ['lt', 0]; //扣分
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();
            $week_time_1 = $start + 3600 * 24 * 7;
            $week_time_2 = $week_time_1 + 3600 * 24 * 7;
            $week_time_3 = $week_time_2 + 3600 * 24 * 7;
            foreach ($res_people as $key => $value) {
                if ($value['prize_buckle_time'] < $week_time_1) {
                    $res_people[$key]['week'] = 1;
                } elseif ($value['prize_buckle_time'] >= $week_time_1 && $value['prize_buckle_time'] < $week_time_2) {
                    $res_people[$key]['week'] = 2;
                } elseif ($value['prize_buckle_time'] >= $week_time_2 && $value['prize_buckle_time'] < $week_time_3) {
                    $res_people[$key]['week'] = 3;
                } else {
                    $res_people[$key]['week'] = 4;
                }
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            for ($i = 1; $i <= 4; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['week'] == $i) {
                        $data_array[] = $v;
                        $prize += $v['integral_b'];
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '周',
                    'integral_type' => $sys['integral_type'], //奖分类型 0周任务  1月任务
                    'integral_reward' => $sys['integral_deduct'], //奖分目标
                    'data_array' => $data_array
                ];
            }
        } elseif ($type == 3) {//天类型
            if(empty($time)){
                $time = date('Y-m',time());
            }
            $start = strtotime($time . '-01');
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $where['a.integral_b'] = ['lt', 0]; //扣分
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();
            foreach ($res_people as $key => $value) {
                $res_people[$key]['day'] = (int)date('d', $value['prize_buckle_time']);
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            $day = (int)date('d', strtotime('+1 month -1 day', strtotime($time . '-01')));
            for ($i = 1; $i <= $day; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['day'] == $i) {
                        $data_array[] = $v;
                        $prize += $v['integral_b'];
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '日',
                    'integral_type' => $sys['integral_type'], //奖分类型 0周任务  1月任务
                    'integral_reward' => $sys['integral_deduct'], //奖分目标
                    'data_array' => $data_array
                ];
            }
        }
        $users = M('users a')->join('com_department b','b.id=a.department_id','left')
            ->where('a.id',$uid)
            ->field('a.name,b.name department_name,a.job_number')->find();
//        var_dump($data);die;
        //当前月份奖扣详情
        $where = [];
        $time = date('Y-m',time());
        $start = strtotime($time . '-01');
        $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.status'] = 2; //审核通过
        $where['a.integral_b'] = ['lt', 0]; //扣分
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate(10, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $sum = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->sum('a.integral_b');
        $count_page = ceil($count / 10);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $month_data = [
            'list' => $list,
            'page' => 1,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>10,
            'sum'=>$sum,
        ];
        $this->assign('month_data',$month_data);
        $this->assign('data', $data);
        $this->assign('users', $users);
        return $this->fetch();
    }
    //扣分详情接口数据
    public function buckle_info_api(){
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $type = I('type',1);//类型  1月份  2周  3天
        $time = I('time','');
        $number = I('number','');
        $page = I('page',1);
        $page_limit = I('page_limit',10);
        if(empty($type)){
            return return_json(1,'类型不能为空');
        }
        if(empty($time)){
            return return_json(1,'时间不能为空');
        }
        if(empty($number)){
            return return_json(1,'数字不能为空');
        }
        $where = [];
        if($type == 1){
            $start = strtotime($time . '-'.$number);
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        }elseif($type == 2){
            if($number == 1){
                $start = strtotime($time . '-01');
                $end = $start + 3600 * 24 * 7;
            }elseif ($number == 2){
                $start = strtotime($time . '-01') + 3600 * 24 * 7;
                $end = $start + 3600 * 24 * 7;
            }elseif ($number == 3){
                $start = strtotime($time . '-01') + 3600 * 24 * 14;
                $end = $start + 3600 * 24 * 21;
            }elseif ($number == 4){
                $start = strtotime($time . '-01') + 3600 * 24 * 21;
                $end = strtotime('+1 month -1 day',strtotime($time . '-01')) + 24 * 3600 - 1;
            }

        }elseif ($type == 3){
            $start = strtotime($time . '-'.$number);
            $end = $start + 3600 * 24 -1;
        }
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.status'] = 2; //审核通过
        $where['a.integral_b'] = ['lt', 0]; //扣分
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate($page_limit, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $count_page = ceil($count / $page_limit);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $data = [
            'list' => $list,
            'page' => (int)$page,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>$page_limit,
        ];
        return return_json(0, '获取成功', $data);

    }
    //人次任务详情
    public function people_info()
    {
        $type = I('type', 1); //类型 1月  2周  3天  默认1
        $time = I('time',''); //默认本年时间
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b', 'a.prize_buckle_id=b.id', 'left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        if ($type == 1) {//月类型
            if(empty($time)){
                $time = date('Y',time());
            }
            $start = strtotime($time . '-01-01');
            $end = strtotime($time . '-12-31') + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();

            foreach ($res_people as $key => $value) {
                $res_people[$key]['month'] = (int)date('m', $value['prize_buckle_time']);
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            for ($i = 1; $i <= 12; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['month'] == $i) {
                        $prize ++;
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //人次
                    'desc_time' => $time . '-' . $i . '月',
                    'man_type' => $sys['man_type'], //1日任务  2周任务  3月任务
                    'man_num' => $sys['man_num'], //人次目标
                    'data_array' => $data_array
                ];
            }
        } elseif ($type == 2) {//周类型
            if(empty($time)){
                $time = date('Y-m',time());
            }
            $start = strtotime($time . '-01');
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();
            $week_time_1 = $start + 3600 * 24 * 7;
            $week_time_2 = $week_time_1 + 3600 * 24 * 7;
            $week_time_3 = $week_time_2 + 3600 * 24 * 7;
            foreach ($res_people as $key => $value) {
                if ($value['prize_buckle_time'] < $week_time_1) {
                    $res_people[$key]['week'] = 1;
                } elseif ($value['prize_buckle_time'] >= $week_time_1 && $value['prize_buckle_time'] < $week_time_2) {
                    $res_people[$key]['week'] = 2;
                } elseif ($value['prize_buckle_time'] >= $week_time_2 && $value['prize_buckle_time'] < $week_time_3) {
                    $res_people[$key]['week'] = 3;
                } else {
                    $res_people[$key]['week'] = 4;
                }
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            for ($i = 1; $i <= 4; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['week'] == $i) {
                        $prize ++;
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '周',
                    'man_type' => $sys['man_type'], //1日任务  2周任务  3月任务
                    'man_num' => $sys['man_num'], //人次目标
                    'data_array' => $data_array
                ];
            }
        } elseif ($type == 3) {//天类型
            if(empty($time)){
                $time = date('Y-m',time());
            }
            $start = strtotime($time . '-01');
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
            $where['c.first_check_user_id'] = $uid; //初审人
            $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where['c.status'] = 2; //审核通过
            $res_people = M('com_integral_event_relation_users a')
                ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
                ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
                ->join('users d', 'd.id=a.user_id', 'left')
                ->join('users f', 'f.id=c.first_check_user_id', 'left')
                ->join('users g', 'g.id=c.last_check_user_id', 'left')
                ->join('users h', 'h.id=c.record_user_id', 'left')
                ->join('com_event e', 'e.id=b.event_id', 'left')
                ->where($where)
                ->order('c.prize_buckle_time desc')
                ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
                ->select();
            foreach ($res_people as $key => $value) {
                $res_people[$key]['day'] = (int)date('d', $value['prize_buckle_time']);
                $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
            }
            $day = (int)date('d', strtotime('+1 month -1 day', strtotime($time . '-01')));
            for ($i = 1; $i <= $day; $i++) {
                $data_array = [];
                $prize = 0;
                foreach ($res_people as $v) {
                    if ($v['day'] == $i) {
                        $prize ++;
                    }
                }
                $data[] = [
                    'time' => $i,
                    'prize' => $prize, //奖分
                    'desc_time' => $time . '-' . $i . '日',
                    'man_type' => $sys['man_type'], //1日任务  2周任务  3月任务
                    'man_num' => $sys['man_num'], //人次目标
                    'data_array' => $data_array
                ];
            }
        }
        $users = M('users a')->join('com_department b','b.id=a.department_id','left')
            ->where('a.id',$uid)
            ->field('a.name,b.name department_name,a.job_number')->find();
//        var_dump($data);die;
        //当前月份奖扣详情
        $where = [];
        $time = date('Y-m',time());
        $start = strtotime($time . '-01');
        $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.status'] = 2; //审核通过
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate(10, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $sum = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count('a.integral_b');
        $count_page = ceil($count / 10);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $month_data = [
            'list' => $list,
            'page' => 1,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>10,
            'sum'=>$sum,
        ];
        $this->assign('month_data',$month_data);
        $this->assign('data', $data);
        $this->assign('users', $users);
        return $this->fetch();
    }
    //人次详情接口数据
    public function people_info_api(){
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $type = I('type',1);//类型  1月份  2周  3天
        $time = I('time','');
        $number = I('number','');
        $page = I('page',1);
        $page_limit = I('page_limit',10);
        if(empty($type)){
            return return_json(1,'类型不能为空');
        }
        if(empty($time)){
            return return_json(1,'时间不能为空');
        }
        if(empty($number)){
            return return_json(1,'数字不能为空');
        }
        $where = [];
        if($type == 1){
            $start = strtotime($time . '-'.$number);
            $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        }elseif($type == 2){
            if($number == 1){
                $start = strtotime($time . '-01');
                $end = $start + 3600 * 24 * 7;
            }elseif ($number == 2){
                $start = strtotime($time . '-01') + 3600 * 24 * 7;
                $end = $start + 3600 * 24 * 7;
            }elseif ($number == 3){
                $start = strtotime($time . '-01') + 3600 * 24 * 14;
                $end = $start + 3600 * 24 * 21;
            }elseif ($number == 4){
                $start = strtotime($time . '-01') + 3600 * 24 * 21;
                $end = strtotime('+1 month -1 day',strtotime($time . '-01')) + 24 * 3600 - 1;
            }

        }elseif ($type == 3){
            $start = strtotime($time . '-'.$number);
            $end = $start + 3600 * 24 -1;
        }
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.status'] = 2; //审核通过
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate($page_limit, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $count_page = ceil($count / $page_limit);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $data = [
            'list' => $list,
            'page' => (int)$page,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>$page_limit,
        ];
        return return_json(0, '获取成功', $data);

    }

    //比例任务详情
    public function rate_info()
    {
        $time = I('time', date('Y', time())); //默认本年时间
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b', 'a.prize_buckle_id=b.id', 'left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        $start = strtotime($time . '-01-01');
        $end = strtotime($time . '-12-31') + 24 * 3600 - 1;
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.status'] = 2; //审核通过
        $res_people = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->select();
        foreach ($res_people as $key => $value) {
            $res_people[$key]['month'] = (int)date('m', $value['prize_buckle_time']);
            $res_people[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        for ($i = 1; $i <= 12; $i++) {
            $data_array = [];
            $prize = 0;
            $buckle = 0;
            foreach ($res_people as $v) {
                if ($v['month'] == $i) {
                    if($v['integral_b'] >0){
                        $prize+=$v['integral_b'];
                    }else{
                        $buckle+=$v['integral_b'];
                    }
                }

            }
            if($prize == 0 || $buckle == 0){
                $rate = 0;
            }else{
                $rate = abs(round($buckle/$prize,3))*100;
            }
            //预计扣分
            if($rate >= $sys['rate']){
                $rate_del = 0;
                $real_del = 0;
            }else{
                $rate_del = ceil(($sys['rate'] - $rate)/100*$prize);
                if((int)date('m',time()) >= $i){
                    $real_del = 0;
                }else{
                    $real_del = $rate_del;
                }
            }
            $data[] = [
                'time' => $i,
                'prize' => $prize, //奖分
                'buckle' => $buckle, //扣分
                'rate' => $rate, //完成比例
                'desc_time' => $time . '-' . $i . '月',
                'rate_target' => $sys['rate'], //比例目标
                'rate_del' => $rate_del, //预计扣分
                'real_del' => $real_del, //实际扣分
                'data_array' => $data_array
            ];
        }
        //当前月份奖扣详情
        $where = [];
        $time = date('Y-m',time());
        $start = strtotime($time . '-01');
        $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.status'] = 2; //审核通过
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate(10, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $res_people = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->field('a.integral_b')
            ->select();
        $prize = 0;
        $buckle = 0;
        foreach ($res_people as $v) {
            if($v['integral_b'] >0){
                $prize+=$v['integral_b'];
            }else{
                $buckle+=$v['integral_b'];
            }
        }
        if($prize == 0 || $buckle == 0){
            $sum = 0;
        }else{
            $sum = abs(round($buckle/$prize,3))*100;
        }
        $count_page = ceil($count / 10);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $month_data = [
            'list' => $list,
            'page' => 1,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>10,
            'sum'=>$sum,
        ];
        $users = M('users a')->join('com_department b','b.id=a.department_id','left')
            ->where('a.id',$uid)
            ->field('a.name,b.name department_name,a.job_number')->find();
        $this->assign('month_data',$month_data);
//        var_dump($data);die;
        $this->assign('data', $data);
        $this->assign('users', $users);
        return $this->fetch();
    }
    //比例详情接口数据
    public function rate_info_api(){
        $uid = I('uid',0);
        if(empty($uid)){
            $uid = $this->user_id;
        }
        $time = I('time','');
        $number = I('number','');
        $page = I('page',1);
        $page_limit = I('page_limit',10);
        if(empty($time)){
            return return_json(1,'时间不能为空');
        }
        if(empty($number)){
            return return_json(1,'数字不能为空');
        }
        $where = [];
        $start = strtotime($time . '-'.$number);
        $end = strtotime('+1 month -1 day', $start) + 24 * 3600 - 1;
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.status'] = 2; //审核通过
        $res = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('users d', 'd.id=a.user_id', 'left')
            ->join('users f', 'f.id=c.first_check_user_id', 'left')
            ->join('users g', 'g.id=c.last_check_user_id', 'left')
            ->join('users h', 'h.id=c.record_user_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b
                ,f.name first_name,g.name last_name,h.name record_name,c.status')
            ->paginate($page_limit, false, page_param());
        $count = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b', 'b.id=a.event_id', 'left')
            ->join('com_integral_prize_buckle c', 'c.id=b.prize_buckle_id', 'left')
            ->join('com_event e', 'e.id=b.event_id', 'left')
            ->where($where)
            ->count();
        $count_page = ceil($count / $page_limit);
        $list = $res->items();
        foreach ($list as $key => $value) {
            $list[$key]['prize_buckle_time'] = date('Y-m-d', $value['prize_buckle_time']);
        }
        $data = [
            'list' => $list,
            'page' => (int)$page,
            'count_page' => $count_page,
            'count' => $count,
            'page_limit'=>$page_limit,
        ];
        return return_json(0, '获取成功', $data);

    }


}
